package com.zero.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.entity.*;
import com.zero.mapper.*;
import com.zero.entity.Condition;
import com.zero.util.DateUtils;
import com.zero.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class IncomeService {

    @Autowired
    private AssetsDebtService assetsDebtService;

    @Autowired
    private IncomeMapper incomeMapper;

    @Autowired
    private IncomeTypeMapper incomeTypeMapper;

    @Autowired
    private FamilyMapper familyMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Transactional
    public Result add(Income income, HttpServletRequest request){
        if (income == null)
            return Result.fail("获取不到添加的信息");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到个人信息");

        //获取用户的具体信息
        familyMember  = familyMemberMapper.selectById(familyMember.getId());
        income.setMemberId(familyMember.getId());
        income.setFamilyId(familyMember.getFamilyId());

        //更新用户资产流入
        familyMember.setAssets(familyMember.getAssets().add(income.getMoney()));
        familyMemberMapper.update(familyMember);

        //更新家庭资产流入
        Family family = familyMapper.selectById(familyMember.getFamilyId());
        family.setAssets(family.getAssets().add(income.getMoney()));
        familyMapper.update(family);


        int row = incomeMapper.add(income);
        if (row == 1)
            return Result.ok("添加收入信息成功");
        return Result.fail("添加收入信息失败");
    }

    @Transactional
    public Result delete(String ids, HttpServletRequest request){
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到个人信息");
        familyMember  = familyMemberMapper.selectById(familyMember.getId());
        Family family = familyMapper.selectById(familyMember.getFamilyId());
        if (ids == null)
            return Result.fail("获取不到要删除的记录");
        String[] idSet = ids.split(",");
        int row = 0;

        //删除对应的收入记录，并更新家庭和个人的资产流入
        for (String id : idSet) {
            Income income = incomeMapper.selectById(Integer.parseInt(id));
            familyMember.setAssets(familyMember.getAssets().subtract(income.getMoney()));
            familyMemberMapper.update(familyMember);
            family.setAssets(family.getAssets().subtract(income.getMoney()));
            familyMapper.update(family);


            row += incomeMapper.delete(Integer.parseInt(id));
        }
        if (row > 0)
            return Result.ok("成功删除"+ row +"条记录");
        return Result.fail("删除失败");
    }

    @Transactional
    public Result update(Income income, HttpServletRequest request){
        if (income == null)
            return Result.fail("获取不到修改的信息");
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到个人信息");
        familyMember  = familyMemberMapper.selectById(familyMember.getId());
        Family family = familyMapper.selectById(familyMember.getFamilyId());

        //更新家庭和个人的资产流入
        Income oldIncome = incomeMapper.selectById(income.getId());
        familyMember.setAssets(familyMember.getAssets().subtract(oldIncome.getMoney()).add(income.getMoney()));
        family.setAssets(family.getAssets().subtract(oldIncome.getMoney()).add(income.getMoney()));
        familyMapper.update(family);
        familyMemberMapper.update(familyMember);

        int row = incomeMapper.update(income);
        if (row == 1)
            return Result.ok("修改收入信息成功");
        return Result.fail("修改收入信息失败");
    }

    public Result select(Condition condition, HttpServletRequest request){

        log.info(condition.toString());
        if (condition == null)
            return Result.fail("获取不到具体的查询信息");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());

        //设置分页查询信息
        condition.setFamilyId(familyMember.getFamilyId());
        if (condition.getPage() != null){
            if (condition .getLimit() != null)
                PageHelper.startPage(condition.getPage(), condition.getLimit());
            else
                PageHelper.startPage(condition.getPage(), 10);
        }else{
            PageHelper.startPage(1, 10);
        }

        List<Income> incomes = incomeMapper.selectByCondition(condition);
        for (Income temp : incomes) {
            temp.setFamilyMember(familyMemberMapper.selectById(temp.getMemberId()));
            if (temp.getMemberId() == familyMember.getId())
                temp.setFlag(0);
            else
                temp.setFlag(1);
            temp.setIncomeType(incomeTypeMapper.selectById(temp.getTypeId()));

        }

        PageInfo pageInfo = new PageInfo(incomes);
        return Result.ok(incomes, pageInfo.getTotal());
    }

    public List<Income> getMemberTodayData(Integer memberId){
        if (memberId == null)
            return null;
        Income incomeParam = new Income();
        incomeParam.setMemberId(memberId);
        incomeParam.setCreateDate(DateUtils.getToday());
        List<Income> result = incomeMapper.select(incomeParam);
        return result;
    }

    public List<Income> getFamilyTodayData(Integer familyId){
        if (familyId == null)
            return null;
        Income incomeParam = new Income();
        incomeParam.setFamilyId(familyId);
        incomeParam.setCreateDate(DateUtils.getToday());
        List<Income> result = incomeMapper.select(incomeParam);
        return result;
    }
}
